package com.centling.smartSealForPhone.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.bean.LoginBean;
import com.centling.smartSealForPhone.constants.SPConstants;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.SPUtil;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.centling.smartSealForPhone.utils.TextLengthWatch;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * 换章申请页面的Activity
 */
public class SealChangeApplyActivity extends TitleBarActivity implements View.OnClickListener {
    private EditText etNewName;
    private EditText etReason;
    private TextView wordsLimit;
    private TextView btnSure;

    private String loginJson;
    private LoginBean loginInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seal_change_apply);
        setTitleBarText("换章申请");
        loginJson = SPUtil.getString(SPConstants.LOGIN_JSON);
        loginInfo = new Gson().fromJson(loginJson, LoginBean.class);
        initView();
    }

    public void initView() {
        etNewName = (EditText) findViewById(R.id.seal_change_apply_new);
        etReason = (EditText) findViewById(R.id.seal_change_apply_reason);
        btnSure = (TextView) findViewById(R.id.seal_change_apply_sure);
        wordsLimit = (TextView)findViewById(R.id.tv_seal_change_apply_words_limit);
        etNewName.setText(loginInfo.getEquipRegisterInfo().getSealName());
        btnSure.setOnClickListener(this);
        etReason.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)});//限制字数

        etReason.addTextChangedListener(new TextLengthWatch(100));//字数提醒

        etReason.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                wordsLimit.setText(String.format(getString(R.string.number_limit_100, s.toString().length())));
                if (s.toString().length()==100) {
                    wordsLimit.setTextColor(Color.RED);
                }else {
                    wordsLimit.setTextColor(Color.BLACK);
                }

            }
        });
    }

    @Override
    public void onClick(View view) {
        String newName = etNewName.getText().toString();
        String reason = etReason.getText().toString();
        if (newName == null || "".equals(newName)) {
            ShowToast.shortTime("新印章名不能为空");
        } else if (reason == null || "".equals(reason)) {
            ShowToast.shortTime("换章原因不能为空");
        } else {
            Map<String, String> param = new HashMap<>();
            param.put("newSealName", newName);
            param.put("reason", reason);
            postJsonEnqueue(UrlConstants.SEAL_CHANGE_APPLY, param, new OkHttpUtil.NetCallBack() {
                @Override
                public void onSucceed(String json) {
                    ShowToast.shortTime("提交申请成功,等待审核");
                    finish();
                }

                @Override
                public void onFailed(String reason) {
                    ShowToast.shortTime(reason);
                }
            });
        }
    }
}
